// pages/login/login.js
const app = getApp()
const api = wx.getStorageSync('api')
Page({
  data: {
    type: 'login', // 登陆注册切换
    isPwd: false, // 使用密码登陆
    time: 0, // 验证码发送倒计时
    phone: '', // 手机号
    code: '', // 短信验证码
    password: '' // 密码
  },

  /**
   * 切换登陆注册
   */
  toggleType() {
    let type = this.data.type
    this.setData({
      type: type == 'login' ? 'register' : 'login'
    })
  },

  /**
   * 切换验证码和密码登陆
   */
  togglePwd() {
    let isPwd = this.data.isPwd
    this.setData({
      isPwd: isPwd ? false : true
    })
  },

  /**
   * 手机号正则校验
   */
  isPoneAvailable(phone) {
    var myreg = /^1[3-9]\d{9}$/;
    return myreg.test(phone)
  },

  /**
   * 设置定时器
   */
  setTimer() {
    this.setData({
      time: 60
    })
    let that = this
    var timer = setInterval(function () {
      that.setData({
        time: that.data.time - 1
      })
      if (that.data.time < 1) {
        clearInterval(that.data.interval);
      }
    }, 1000);

    this.setData({
      interval: timer
    })
  },

  /**
   * 发送短信
   */
  sendNote() {
    const phone = this.data.phone
    if (!this.isPoneAvailable(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }

    // 手机号输入正确，开始发送请求
    const url = api.sendSms + `?phone=${phone}`
    app.$http.post(url).then(res => {
      console.log(res)
      if (res.code != 500) {
        wx.showToast({
          // title: `已向${phone}发送短信，请注意查收`,
          title: `${phone},${res.message}`,
          icon: 'none',
          duration: 2000
        })
        this.setTimer()
      }
    })
  },

  /**
   * 输入绑定--手机号
   */
  inputPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },

  /**
   * 输入绑定--验证码
   */
  inputCode(e) {
    // let reg = /^[0-9]*$/
    // console.log(reg.test(e.detail.value))
    // if (!reg.test(e.detail.value)) {
    //   console.log('不符合')
    //   return
    // } 
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 输入绑定--密码
   */
  inputPwd(e) {
    this.setData({
      password: e.detail.value
    })
  },

  /**
   * 发送登陆请求
   * @param {*} phone 手机
   * @param {*} code  密码/验证码
   * @param {*} type  0--短信验证码 1--密码登陆
   */
  loginSubmit(phone, code, type) {
    let url = api.login
    let data = {
      code,
      phone,
      type,
      loginType: 'WX'
    }
    app.$http.post(url, data).then(res => {
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000
      })
      console.log(res)
      if (res.code == 200) {
        this.loginSuccess(res.data.user, res.data.token)
      }
    })
  },


  /**
   * 登陆前校验表单
   */
  login() {
    const password = this.data.password
    const code = this.data.code
    const phone = this.data.phone
    if (!this.loginCheck(phone, code, password)) {
      wx.showToast({
        title: '请完善登陆信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    // 登陆提交
    if (this.data.isPwd) {
      this.loginSubmit(phone, password, 1)
    } else {
      this.loginSubmit(phone, code, 0)
    }
  },

  /**
   * 验证登陆表单
   */
  loginCheck(phone, code, password) {
    const isPwd = this.data.isPwd
    if (phone.length == 0 || (code.length == 0 && password.length == 0)) {
      return false
    }
    if (isPwd && password.length == 0) return false
    if (!isPwd && code.length == 0) return false
    return true
  },

  /**
   * 注册提交
   */
  register() {
    const password = this.data.password
    const code = this.data.code
    const phone = this.data.phone
    if (password.length == 0 || code.length == 0 || phone.length == 0) {
      wx.showToast({
        title: '请完善注册信息',
        icon: 'none',
        duration: 2000
      })
      return
    }
    const url = api.register
    const data = {
      code: code,
      password: password,
      phone: phone
    }
    app.$http.post(url, data).then(res => {
      console.log(res)
      wx.showToast({
        title: res.message,
        icon: 'none',
        duration: 2000
      })
      if (res.code == 200) {
        console.log('注册成功，前往主页')
        this.loginSuccess(res.data.user, res.data.token)
      }
    })
  },

  /**
   * 登陆成功
   */
  loginSuccess(userInfo, token) {
    console.log('success');
    wx.setStorageSync('user', userInfo)
    wx.setStorageSync('token', token)
    const user = wx.getStorageSync('user')
    console.log(user)
    setTimeout(function () {
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }, 1500)
  }
})